package menu;

import graphics.ImageHandler;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import core.Constants;
import core.GameOptions;
import core.MessageHandler;
import core.PlayerData;
import core.SingleButtonGame;

public class ConfigurationMenu implements KeyListener{

	public final Color[] PLAYERCOLORS = {Color.green,Color.red,Color.blue,Color.cyan,Color.magenta,Color.yellow,Color.orange,Color.pink,   Color.green,Color.red,Color.blue,Color.cyan,Color.magenta,Color.yellow,Color.orange,Color.pink};
	Vector<PlayerData> players = new Vector<PlayerData>();
	Vector<PlayerMenu> playerMenus = new Vector<PlayerMenu>();
	public enum MenuSelectionStatus {SELECTED, ROW, DESELECTED};
	
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	
	/**
	 * Called once every frame.
	 **/
	public void tick(){
		for(PlayerMenu pm : playerMenus)
			pm.tick();
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		char c = e.getKeyChar();
		switch(c){
		case ' ': //start			
			if(players.isEmpty())
				MessageHandler.showMessage("Please add at least one player by pressing any key.", "iconInfo.png.png");
			else
				SingleButtonGame.getInstance().initGame(players);
			break;
		default:  //key is not bound to an action.
			boolean playerFound = false;
			for(PlayerData p : players){
				if(p.getKey() == c){
					playerFound = true;
					playerMenus.get(players.indexOf(p)).click();
					
				}
			}
			if(!playerFound && players.size() < GameOptions.expectedMaxPlayers){
				int topBorderHeight = 0;
				//int rows = 4;
				
				int rows = (int)(0.5+Math.sqrt(GameOptions.expectedMaxPlayers));
				int columns = (int)(0.99+((double)GameOptions.expectedMaxPlayers/(double)rows));
				
				int playerNumber = players.size();
				PlayerData newPlayer = new PlayerData(c);
				//newPlayer.setPlayerColor(PLAYERCOLORS[playerNumber%PLAYERCOLORS.length]);
				//newPlayer.setPlayerColor(new Color(0+(int)(Math.random()*255), 0+(int)(Math.random()*255), 0+(int)(Math.random()*255)));
				
				double rand = Math.random()*2*Math.PI;
				int r = (int)(Math.sin(rand)*127)+128%255;
				int g = (int)(Math.sin(rand+2)*127)+128%255;
				int b = (int)(Math.sin(rand+4)*127)+128%255;
				newPlayer.setPlayerColor(new Color(r, g, b));
				
				SingleButtonGame.getInstance().gameMode.setupPlayerData(newPlayer);
				players.add(newPlayer);
				PlayerMenu newMenu = new PlayerMenu(newPlayer);
				int offsetX = screenSize.width/columns*(playerNumber%columns);
				int offsetY = (screenSize.height-topBorderHeight)/rows*(playerNumber/columns);
				//newMenu.setOffset(new Point(screenSize.width/(GameOptions.expectedMaxPlayers/rows)*(playerNumber%(GameOptions.expectedMaxPlayers/rows)), topBorderHeight+((screenSize.height-topBorderHeight)/rows)*(playerNumber/(GameOptions.expectedMaxPlayers/rows))));
				newMenu.setOffset(new Point(offsetX, offsetY));
				newMenu.setWidth(screenSize.width/columns);
				newMenu.setHeight((screenSize.height-topBorderHeight)/rows);
				playerMenus.add(newMenu);
				
				
				
			}
			break;
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	
	public void paint(Graphics g){

		g.setColor (Color.white);
		g.fillRect (0, 0, screenSize.width, screenSize.height);
		
		g.setColor(Color.black);
		g.setFont(Constants.defaultFont);
		g.drawString("Press any keys to be used by the players. Avoid keys that can't be writtern, such as Ctrl, Shift and Caps", 300, 400);
		//g.drawString("Expect a small freeze.", 300, 420);
		g.drawString("Press space to start.", 300, 420);
			
		for(PlayerMenu m : playerMenus){
			m.paint(g);
		}
	}
}
